package com.killer.auth.config;

import com.killer.auth.util.AdminTokenUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author Killer
 * @title: TokenRequestInterceptor
 * @projectName shop-parent
 * @description: TODO
 * @date 2021/11/1 20:01
 */
@Configuration
@Component
public class TokenRequestInterceptor implements RequestInterceptor {

    @Value("${encrypt.key-store.location}")
    private String location;
    @Value("${encrypt.key-store.secret}")
    private String secret;
    @Value("${encrypt.key-store.alias}")
    private String alias;
    @Value("${encrypt.key-store.password}")
    private String password;


    /**
     * feign请求调用之前进行拦截
     * @param template
     */
    @Override
    public void apply(RequestTemplate template) {
        //生成管理员令牌
        String token = AdminTokenUtil.createAdminToken(location,secret,password,alias);
        //放入feign请求头中
        template.header("Authorization","bearer "+token);
    }
}
